<?php

function get_category_info($id,$module = '') {
    global $db;
    $data = array();
    $query = $db->query("SELECT id,name,module,page,parameter,link,display_menu,display_list,order_by
                            FROM system_category
                            WHERE parent_id='{$id}' ".($id == 0 || $module=='' ? '' : "AND module='{$module}'")."
                            ORDER BY order_by,id ASC");

    while ($row = $db->fetch_array($query)) {
        $row['status'] = 'close';
        $row['child'] = intval($db->result_first("SELECT count(*) FROM system_category 
                WHERE parent_id='{$row['id']}' ".($module=='' ? '' : "AND module='{$module}'" )."
                ORDER BY order_by,id ASC"));
        $data[] = $row;
    }
    return $data;
}

$module = empty($module) ? '' : trim($module);
$data = get_category_info($id,$module);
if ($id == 0) {
    $data[0]['status'] = 'open';
    $data[0]['children'] = get_category_info($data[0]['id'],$module);
}
echo array2json($data, true);
exit();
?>
